package app.web.admin.controller;

import app.web.admin.domain.ao.LoginAo;
import app.web.admin.domain.ao.PermissionMenusAo;
import app.web.admin.domain.ao.UserCreateAo;
import app.web.admin.domain.ao.UserPatchByIdAo;
import app.web.admin.domain.pojo.Admin;
import app.web.admin.domain.vo.MenuVo;
import app.web.admin.service.AdminService;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.gitee.feizns.quickstart.domain.page.PageRecordsVo;
import com.gitee.feizns.quickstart.domain.vo.Ret;
import com.gitee.feizns.quickstart.web.domain.query.QueryParamsAo;
import com.gitee.feizns.quickstart.web.restful.Rests;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 管理模块/管理员
 * @author feizns
 * @since 2025-04-09
 */
@Validated
@SaCheckLogin
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/admin")
public class AdminController {

    /**
     * 管理员服务
     */
    private final AdminService service;

    /**
     * 登录验证码
     * @return {@link Ret }<{@link String }>
     */
    @SaIgnore
    @GetMapping("/login-captcha")
    public Ret<String> loginCaptcha() {
        return Ret.ok(service.loginCaptcha());
    }

    /**
     * 登录
     * @param ao 请求参数
     * @return {@link Ret }<{@link String }>
     */
    @SaIgnore
    @PostMapping("/login")
    public Ret<String> login(@Validated @RequestBody LoginAo ao) {
        return Ret.ok(service.login(ao));
    }

    /**
     * 注销
     * @return {@link Ret }<{@link Admin }>
     */
    @DeleteMapping("/logout")
    public Ret<Void> logout() {
        return Ret.ok(service::logout);
    }

    /**
     * 用户信息
     * @return {@link Ret }<{@link Admin }>
     */
    @GetMapping("/userinfo")
    public Ret<Admin> userinfo() {
        return Ret.ok(service.userinfo());
    }

    /**
     * 权限菜单
     * @param ao 请求参数
     * @return {@link Ret }<{@link Void }>
     */
    @GetMapping("/permission-menus")
    public Ret<List<MenuVo>> permissionMenus(PermissionMenusAo ao) {
        return Ret.ok(service.permissionMenus(ao));
    }

    /**
     * 分页
     * @param query 查询参数
     * @return {@link Ret}<{@link PageRecordsVo}<{@link Admin}>>
     */
    @SaIgnore
    @GetMapping
    public Ret<PageRecordsVo<Admin>> page(QueryParamsAo query) {
        return Ret.ok(Rests.page(Admin.class, query.page(), query));
    }

    /**
     * 新增
     * @param ao 请求参数
     * @return {@link Ret}<{@link Admin}>>
     */
    @PostMapping
    public Ret<Admin> create(@Validated @RequestBody UserCreateAo ao) {
        return Ret.ok(service.create(ao));
    }

    /**
     * 详情
     * @param id 编号
     * @return {@link Ret}<{@link Admin}>
     */
    @GetMapping("/{id}")
    public Ret<Admin> getById(@PathVariable Long id) {
        return Ret.ok(Rests.getById(Admin.class, id));
    }

    /**
     * 修改
     * @param id 编号
     * @param ao 请求参数
     * @return {@link Ret}<{@link Admin >
     */
    @PatchMapping("/{id}")
    public Ret<Admin> patchById(@PathVariable Long id, @Validated @RequestBody UserPatchByIdAo ao) {
        return Ret.ok(service.patchById(id, ao));
    }

    /**
     * 删除
     * @param id 编号
     * @return {@link Ret}<{@link Admin}>
     */
    @DeleteMapping("/{id}")
    public Ret<Admin> deleteById(@PathVariable Long id) {
        return Ret.ok(service.deleteById(id));
    }

}
